<?php
class ControllerStartupSass extends Controller {
	public function index() {
        $theme_dir = $this->getCurrentThemeDir();

        $file = DIR_APPLICATION . 'view/theme/' . $theme_dir . '/stylesheet/bootstrap.css';

		if (!is_file($file) || (is_file(DIR_APPLICATION . 'view/theme/' . $this->config->get('theme_directory') . '/stylesheet/sass/_bootstrap.scss') && !$this->config->get('developer_sass'))) {
			$scss = new \Leafo\ScssPhp\Compiler();
			$scss->setImportPaths(DIR_APPLICATION . 'view/theme/' . $this->config->get('theme_directory') . '/stylesheet/sass/');

			$output = $scss->compile('@import "_bootstrap.scss"');

			$handle = fopen($file, 'w');

			flock($handle, LOCK_EX);

			fwrite($handle, $output);

			fflush($handle);

			flock($handle, LOCK_UN);

			fclose($handle);
		}
	}

    /**
     * @link http://dw.me/index.php?route=startup/sass/dev - 点击编译
     *
     * 测试编译自己编译
     */
    public function dev() {
        $theme = $this->getCurrentThemeDir();

        $dist_dir = DIR_APPLICATION . 'view/theme/' . $theme . '/stylesheet/';
        $dist_name = 'stylesheet.css';
        $dist_file = $dist_dir . $dist_name;
        $map_name = $dist_name . '.map';

        $source_dir = DIR_APPLICATION . 'view/theme/' . $theme . '/scss/';
        $source_entry = 'stylesheet.scss';

        $has_dist = is_file($dist_file);
        $has_source =  is_file($source_dir . $source_entry);
        $use_cache = $this->config->get('developer_sass');
        if (! $has_dist || ($has_source && ! $use_cache)) {
            /**
             * @link http://leafo.github.io/scssphp/docs/ - 参数文档
             */
            $scss = new \Leafo\ScssPhp\Compiler();
            $scss->setImportPaths(DIR_APPLICATION . 'view/theme/' . $theme . '/scss/');
            /**
             * 压缩并去掉注释
             */
            $scss->setFormatter(Leafo\ScssPhp\Formatter\Crunched::class);
            $scss->setSourceMap(\Leafo\ScssPhp\Compiler::SOURCE_MAP_FILE);
            /**
             * @see \Leafo\ScssPhp\SourceMap\SourceMapGenerator::$defaultOptions
             */
            $scss->setSourceMapOptions([
                'sourceMapWriteTo' => $dist_dir . $map_name,
                'sourceMapURL'     => $map_name,
            ]);
            $output = $scss->compile("@import '$source_entry'");

            $handle = fopen($dist_file, 'w');
            flock($handle, LOCK_EX);
            fwrite($handle, $output);

            fflush($handle);

            flock($handle, LOCK_UN);
            fclose($handle);

            echo "compiled!";
        }
    }

    /**
     * @return string
     */
    private function getCurrentThemeDir(): string
    {
        // TODO: 需要通过当前店铺配置动态查找
        // first get current store_id
        // find setting
        //  -> where (store_id, code => 'config_theme')
        //  -> get value
        // use value as theme {{name}}
        //  -> find setting -> where (store_id, value => theme_{{name}}_directory)
        //  -> get value or "default"
        $theme_dir = $this->config->get('theme_default_directory') ?: 'default';

        return $theme_dir;
    }
}
